package com.dgut.coal.controller;

import com.dgut.coal.entity.Purchase;
import com.dgut.coal.entity.Supplier;
import com.dgut.coal.service.PurchaseService;
import com.dgut.coal.service.SupplierService;
import com.dgut.coal.vo.SupplierVO;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

/**
 * @Author: Yang
 * @Description:
 * @Date: Created in 0:29 2019/10/7
 * @Modifued BY:
 */
@Controller
public class ExecutionController {


    @Autowired
    PurchaseService ps;

    @Autowired
    SupplierService ss;

    @RequestMapping("/executionList")
    public String list(Map<String, Object> map, String bill_numer, Integer exec_state, @RequestParam(required = false,defaultValue = "1",value = "pn") Integer pageNum){
        PageHelper.startPage(pageNum,5);
        List<Purchase> list = ps.execList(bill_numer,exec_state);
        PageInfo pageInfo = new PageInfo<>(list,5);
        List keywords = Arrays.asList(bill_numer,exec_state);
        map.put("pageInfo",pageInfo);
        map.put("keywords",keywords);
        return "procurementExecution-list";
    }

    @RequestMapping("/executionFill")
    public String executionFill(Integer apply_id,Map<String,Object> map){
        Purchase purchase = ps.viewPurchase(apply_id);
        List<Supplier> company = ps.contract(apply_id);
        map.put("company",company);
        map.put("purchase",purchase);
        return "procurementExecution-fill";
    }

    @RequestMapping("/executionView")
    public String executionView(Integer apply_id,Map<String, Object> map){
        Purchase purchase = ps.viewPurchase(apply_id);
        List<Supplier> supplierList = ss.realSupplier(apply_id);
        map.put("supplierList",supplierList);
        map.put("purchase",purchase);
        return "procurementExecution-view";
    }

    @RequestMapping("/execFillPass")
    @ResponseBody
    public int execFillPass(Integer apply_id, SupplierVO supplierVO){
        int row = ps.execFillPass(apply_id);
        List<Supplier> suppliers = supplierVO.getSuppliers();
        System.out.println(suppliers);
        ss.passSupplier(suppliers);
        return row;
    }
}
